home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0997.zip / tagged.txt < prev    next >
Text File  |  1997-07-22  |  9KB  |  299 lines

  1. _Tagged Data Storage Architectures_
  2. by Jeremy Vineyard
  3.  
  4. Listing One
  5. class CSizable
  6. {
  7. protected:
  8.     CRect    cs_dimensions;   // Assume we already have a CRect class.
  9. public:
  10.    virtual CRect GetDimensions();
  11.     virtual void Resize(CRect& rNewSize);
  12.     ...
  13. }
  14.  
  15. Listing Two
  16. class FT_TaggableObject
  17. {
  18. public:
  19.     // Basic tag routines.  
  20.     virtual SN_Error AttachTag(FT_TagName tagName, FT_TagType tagType,
  21.                       FT_TagData tagData, FT_TagDataSize tagDataSize);
  22.     virtual SN_Error RemoveTag(FT_TagName tagName);
  23.     virtual SN_Error RemoveAllTags();
  24.     virtual SN_Error RetrieveTag(FT_TagName tagName, FT_Tag*& rpTag);
  25.     virtual SN_Error CountTags(FT_TagIndex& rTagCount);
  26.     ...
  27. };
  28.  
  29. Listing Three
  30. class FT_Tag
  31. {
  32. protected:
  33.     FT_TagData          ftt_tagData;
  34.     FT_TagDataSize      ftt_tagDataSize;
  35.     FT_TagName          ftt_tagName;
  36.     FT_TagType          ftt_tagType;
  37.     ... 
  38. };
  39.  
  40. Listing Four
  41. class TK_PackerObject
  42. {
  43. public:
  44.     virtual SN_Error PackData(LB_DataChunk& rDataChunk);
  45.     virtual SN_Error UnpackData(LB_DataChunk& rPackedData);
  46.     ...
  47. };
  48.  
  49.  
  50. Listing Five
  51. class TK_StorableObject
  52. {
  53. public:
  54.     virtual SN_Error PackData(LB_DataChunk& rDataChunk);
  55.     virtual SN_Error UnpackData(LB_DataChunk& rPackedData);
  56. public:
  57.     virtual SN_Error AttachStorageTags(TK_PackerObject& rObject);
  58.     virtual SN_Error ExtractStorageTags(TK_PackerObject& rObject);
  59. };
  60.  
  61.  
  62. Listing Six
  63. class LB_DataChunk
  64. {
  65. public:
  66.     virtual void* GetDataReference();
  67.     ...
  68. };
  69.  
  70. Listing Seven
  71. class TK_TagHeader
  72. {
  73. public:
  74.     FT_TagType          tkt_tagType;
  75.     FT_TagName          tkt_tagName;
  76.     long                tkt_unused;
  77.     FT_TagDataSize      tkt_tagDataSize;
  78. public: 
  79.     TK_TagHeader();
  80. };
  81.  
  82. Listing Eight
  83. // This is the class ID of CA_ObjectPane.
  84. const long ca_kObjectPane   = TcaOPU;
  85.  
  86. Listing Nine
  87. SN_Error TK_StorableObject::PackData(LB_DataChunk& rDataChunk)
  88. {
  89.     SN_Error            result;
  90.     TK_PackerObject     tempObject;
  91.     
  92.     EX_TRY
  93.     {
  94.         EX_THROW_ERROR(AttachStorageTags(tempObject));
  95.         EX_THROW_ERROR(tempObject.PackData(rDataChunk));
  96.     }
  97.     catch (SN_Exception& rException)
  98.     {
  99.         result = rException;
  100.     }
  101.     return result;  
  102. }
  103.  
  104. Listing Ten
  105. SN_Error CA_ColoredObject::AttachStorageTags(TK_PackerObject& rObject)
  106. {
  107.     SN_Error            result;
  108.     EX_TRY
  109.     {
  110.         EX_THROW_ERROR(TK_AttachRGBColor(rObject, kColorName, 
  111.                                        cac_color.GetRGBColor()));
  112.     }
  113.     catch (SN_Exception& rException)
  114.     {
  115.    }
  116.     return result;
  117. }
  118.  
  119. Listing Eleven
  120. SN_Error TK_PackerObject::PackData(LB_DataChunk& rDataChunk)
  121. {
  122.     SN_Error            result;
  123.     EX_TRY
  124.     {
  125.         // Pack the object's tags into a chunk of data.
  126.         EX_THROW_ERROR(PrepareForPackingData(rDataChunk));
  127.         EX_THROW_ERROR(PackMyData(rDataChunk));
  128.         EX_THROW_ERROR(FinishPackingData(rDataChunk));
  129.     }
  130.     catch (SN_Exception& rException)
  131.     {
  132.         result = rException;
  133.         FailPackingData(rDataChunk);
  134.     }
  135.     return result;
  136. }
  137. SN_Error TK_PackerObject::PackMyData(LB_DataChunk& rDataChunk)
  138. {
  139.     LB_DataSize             totalSize, chunkSize;
  140.     SN_Error                result;
  141.     EX_TRY
  142.     {
  143.         EX_THROW_ERROR(CalculateTotalDataSizeWithHeader(totalSize,
  144.                                                    sizeof(TK_TagHeader)));
  145.         // Resize the chunk of data to fit all of the packed tag 
  146.         // data.
  147.         chunkSize = totalSize;
  148.         rDataChunk.SetDataSize(chunkSize);
  149.         // Pack the object's tags into a chunk of data.
  150.         long            stuffOffset = 0;
  151.         EX_THROW_ERROR(KeepPackingData(rDataChunk, stuffOffset));
  152.     }
  153.     catch (SN_Exception& rException)
  154.     {
  155.         result = rException;
  156.     }
  157.     return result;
  158. }
  159. SN_Error TK_PackerObject::KeepPackingData(LB_DataChunk& rDataChunk, 
  160.                                                        long& rStuffOffset)
  161. {
  162.    TK_TagHeader         tagHeader;
  163.     FT_TagIndex         tagCount;
  164.     FT_Tag*             pTag;
  165.     FT_TagDataSize      headerSize = sizeof(TK_TagHeader);
  166.     SN_Error            result;
  167.     
  168.     // KeepPackingData() stuffs tag info at specified offset into data.
  169.     EX_TRY
  170.     {
  171.         EX_THROW_ERROR(CountTags(tagCount));
  172.         for (FT_TagIndex tagIndex = 1; tagIndex <= tagCount; tagIndex++)
  173.         {
  174.             // Retrieve the current tag.
  175.             EX_THROW_ERROR(RetrieveTagWithIndex(tagIndex, pTag));
  176.             EX_THROW_NIL(pTag);
  177.             // Stuff the tag information into the tag header.
  178.             tagHeader.tkt_tagType = pTag->GetType();
  179.             tagHeader.tkt_tagName = pTag->GetName();
  180.             tagHeader.tkt_tagDataSize = pTag->GetDataSize();
  181.             // Stuff the tag info into the data chunk.
  182.             rDataChunk.StuffAndOffsetData((Ptr) &tagHeader, 
  183.                                                 rStuffOffset, headerSize);
  184.             rDataChunk.StuffAndOffsetData(FT_GetTagDataReference(
  185.              pTag->GetData()), rStuffOffset, pTag->GetDataSize());
  186.         }
  187.     }
  188.     catch (SN_Exception& rException)
  189.     {
  190.         result = rException;
  191.         // The data chunk is no longer valid.
  192.         rDataChunk.DestroyData();
  193.     }
  194.     return result;
  195. }
  196.  
  197. Listing Twelve
  198. void* CreateAnyObject(long objectType)
  199. {
  200.     switch (objectType)
  201.     {
  202.         case ca_kObjectPane:
  203.             return new CA_ObjectPane;
  204.             break;  
  205.         case vp_kView:
  206.             return new VP_View;
  207.             break;  
  208.         ...
  209.         default:
  210.             return nil;
  211.     }
  212. }
  213.  
  214. Listing Thirteen
  215. SN_Error TK_PackerObject::UnpackMyData(LB_DataChunk& rPackedData)
  216. {
  217.     TK_TagHeader*   pTagHeader;
  218.     FT_TagData      tagData = nil;
  219.     SN_Error        result;
  220.     LB_DataSize     unpackOffset = 0;
  221.     LB_DataSize     dataSize = rPackedData.GetDataSize();
  222.     // Unpack the packed data to restore the object. Add each tag as you come 
  223.     // upon it in data chunk. For now, assume all data is packed tag data.
  224.     EX_TRY
  225.     {
  226.         // Make sure that if the data is a handle, it doesn't get purged.
  227.         LB_ChunkData            chunkData;
  228.         EX_THROW_ERROR(rPackedData.RetrieveData(chunkData));
  229.         EX_THROW_NIL(chunkData.GetData());
  230.         
  231.         // Remove all existing tags.
  232.         EX_THROW_ERROR(RemoveAllTags());
  233.         
  234.         // This object has been stored with tags- extract them.
  235.         while (unpackOffset < dataSize)
  236.         {
  237.             // Extract the current tag header.
  238.             pTagHeader = (TK_TagHeader*) ((long) chunkData.GetData() + 
  239.                                                                unpackOffset);
  240.             tagData = nil;
  241.             // Advance past the tag header.
  242.             unpackOffset += sizeof(TK_TagHeader);
  243.             if (pTagHeader->tkt_tagDataSize > 0)
  244.             {
  245.                 // This tag has data. Allocate space in memory for tag data.
  246.                 tagData = GetAllocatedTagData(pTagHeader->tkt_tagDataSize);
  247.                 EX_THROW_NIL(tagData);
  248.                 EX_THROW_NIL(FT_GetTagDataReference(tagData));
  249.                 {
  250.                   FT_UseTagData   useTagData(tagData);
  251.                   // Copy the tag data into the pointer.
  252.                   BlockMove((Ptr) ((long) pTagHeader + sizeof(TK_TagHeader)),
  253.                                       FT_GetTagDataReference(tagData), 
  254.                                       FT_GetTagDataSize(tagData));
  255.                   // Advance past the tag data.
  256.                   unpackOffset +=
  257.                               pTagHeader->tkt_tagDataSize;
  258.                 }
  259.            }
  260.             // Attach each tag.
  261.             EX_THROW_ERROR(AttachTag(pTagHeader->tkt_tagName, 
  262.               pTagHeader->tkt_tagType, tagData, FT_GetTagDataSize(tagData)));
  263.         }
  264.     }
  265.     catch (SN_Exception& rException)
  266.     {
  267.         result = rException;
  268.         if (tagData)
  269.             // Release the unused tag data from memory.
  270.             DisposeTagData(tagData);
  271.     }
  272.     return result;
  273. }
  274.  
  275. Listing Fourteen
  276. SN_Error CA_ColoredObject::ExtractStorageTags(TK_PackerObject& rObject)
  277. {
  278.     SN_Error            result;
  279.     EX_TRY
  280.     {
  281.         RGBColor            rgbColor;
  282.         if (!TK_RetrieveRGBColor(rObject, kColorName, rgbColor).NoError())
  283.             // Use the default value.           
  284.             SetColor(0, 0, 0);      
  285.         else
  286.             cac_color.SetRGBColor(rgbColor);
  287.     }
  288.     catch (SN_Exception& rException)
  289.     {
  290.         result = rException;
  291.     }
  292.     return result;
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299.